<?php

/**
 * @Author: liu
 * @Date:   2017-11-22 10:09:52
 * @Last Modified by:   liu
 * @Last Modified time: 2017-11-29 15:38:43
 * 用于用户经验记录
 */

class UserExperienceFlow extends Eloquent
{
    protected $table = "user_experience_flow";

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2017-11-22T11:48:55+0800
     * @param    int                   $user_id            用户id
     * @param    int                   $shop_id            门店ID
     * @param    string                $order_id           订单号
     * @param    int                   $action             操作类型 1增加 2减少
     * @param    int                   $type               经验类型
     * @param    int                   $number             经验数量
     * @param    string                $note               备注
     * @return   [type]                                       [description]
     */
    public static function record($user_id,$shop_id,$order_id,$action,$type,$number,$note = ''){
        if($number<=0)return;
        
        //记录经验流水
        $uef = new UserExperienceFlow();
        $uef->user_id = $user_id;
        $uef->shop_id = $shop_id;
        $uef->order_id = $order_id;
        $uef->action = $action;
        $uef->type = $type;
        $uef->number = $number;
        $uef->note = $note;
        $uef->save();
        //更新用户经验
        if($action == 1){ //增加减少
            DB::update("update users set experience=experience+? where id=?",array($number,$user_id));
        }else{ //支出
            DB::update("update users set experience=experience-? where id=?",array($number,$user_id));
        }
    }


}